package com.back.service.impl;

import com.back.exception.BusinessException;
import com.back.exception.ErrorCode;
import com.back.exception.ThrowUtils;
import com.back.model.dto.skill.SkillAddRequest;
import com.back.model.dto.skill.SkillQueryRequest;
import com.back.model.dto.skill.SkillUpdateRequest;
import com.back.model.entity.Hero;
import com.back.model.entity.Skill;
import com.back.model.vo.skill.SkillVO;
import com.back.service.HeroService;
import com.back.service.SkillService;
import com.back.mapper.SkillMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @description 针对表【Skill】的数据库操作Service实现
* @createDate 2025-01-21 15:30:00
*/
@Service
public class SkillServiceImpl extends ServiceImpl<SkillMapper, Skill>
    implements SkillService{

    @Resource
    private SkillMapper skillMapper;
    
    @Resource
    private HeroService heroService;

    @Override
    public long addSkill(SkillAddRequest skillAddRequest) {
        ThrowUtils.throwIf(skillAddRequest == null, ErrorCode.PARAMS_ERROR);
        
        Skill skill = new Skill();
        BeanUtils.copyProperties(skillAddRequest, skill);
        
        // 校验技能信息
        validSkill(skill, true);
        
        // 保存到数据库
        boolean result = this.save(skill);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        
        return skill.getSkillId();
    }

    @Override
    public boolean deleteSkill(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        
        // 判断技能是否存在
        Skill oldSkill = this.getById(id);
        ThrowUtils.throwIf(oldSkill == null, ErrorCode.NOT_FOUND_ERROR);
        
        // 删除技能
        boolean result = this.removeById(id);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        
        return true;
    }

    @Override
    public boolean updateSkill(SkillUpdateRequest skillUpdateRequest) {
        ThrowUtils.throwIf(skillUpdateRequest == null || skillUpdateRequest.getSkillId() == null, ErrorCode.PARAMS_ERROR);
        
        Skill skill = new Skill();
        BeanUtils.copyProperties(skillUpdateRequest, skill);
        
        // 校验技能信息
        validSkill(skill, false);
        
        // 判断技能是否存在
        long id = skillUpdateRequest.getSkillId();
        Skill oldSkill = this.getById(id);
        ThrowUtils.throwIf(oldSkill == null, ErrorCode.NOT_FOUND_ERROR);
        
        // 更新技能
        boolean result = this.updateById(skill);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        
        return true;
    }

    @Override
    public SkillVO getSkillVOById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        
        // 查询技能
        Skill skill = this.getById(id);
        ThrowUtils.throwIf(skill == null, ErrorCode.NOT_FOUND_ERROR);
        
        return getSkillVO(skill);
    }

    @Override
    public IPage<SkillVO> listSkillVOByPage(SkillQueryRequest skillQueryRequest) {
        int current = skillQueryRequest.getCurrent();
        int size = skillQueryRequest.getPageSize();
        
        // 查询技能列表
        IPage<Skill> skillPage = skillMapper.listSkillByPage(new Page<>(current, size),
                skillQueryRequest.getSkillName(), skillQueryRequest.getHeroId());
        
        // 转换为VO
        Page<SkillVO> skillVOPage = new Page<>(current, size, skillPage.getTotal());
        List<SkillVO> skillVOList = skillPage.getRecords().stream()
                .map(this::getSkillVO)
                .collect(Collectors.toList());
        skillVOPage.setRecords(skillVOList);
        
        return skillVOPage;
    }

    @Override
    public List<SkillVO> getSkillListByHeroId(Integer heroId) {
        ThrowUtils.throwIf(heroId == null || heroId <= 0, ErrorCode.PARAMS_ERROR);
        
        List<Skill> skillList = skillMapper.listSkillByHeroId(heroId);
        return skillList.stream()
                .map(this::getSkillVO)
                .collect(Collectors.toList());
    }

    @Override
    public void validSkill(Skill skill, boolean add) {
        if (skill == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        
        String skillName = skill.getSkillName();
        Integer heroId = skill.getHeroId();
        
        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(skillName), ErrorCode.PARAMS_ERROR);
            ThrowUtils.throwIf(heroId == null || heroId <= 0, ErrorCode.PARAMS_ERROR);
        }
        
        // 有参数则校验
        if (StringUtils.isNotBlank(skillName) && skillName.length() > 100) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "技能名称过长");
        }
        
        // 校验英雄是否存在
        if (heroId != null && heroId > 0) {
            Hero hero = heroService.getById(heroId);
            ThrowUtils.throwIf(hero == null, ErrorCode.PARAMS_ERROR, "关联的英雄不存在");
        }
    }

    @Override
    public SkillVO getSkillVO(Skill skill) {
        if (skill == null) {
            return null;
        }
        
        SkillVO skillVO = new SkillVO();
        BeanUtils.copyProperties(skill, skillVO);
        
        // 获取关联的英雄名称
        if (skill.getHeroId() != null) {
            Hero hero = heroService.getById(skill.getHeroId());
            if (hero != null) {
                skillVO.setHeroName(hero.getHeroName());
            }
        }
        
        return skillVO;
    }
}